//
//  AVRequest+ChangeInfo.m
//  项目三
//
//  Created by rimi1 on 2016/12/13.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "AVRequest+ChangeInfo.h"

@implementation AVRequest (ChangeInfo)

+ (void)avHeadImageWithData:(NSData *)data headSueeccd:(AVImage)headSueeccd {
    [AVRequest avImageWithData:data succeed:^(NSString *url, BOOL succeed, NSError *error) {
        if (error) {
            headSueeccd(url, succeed, error);
            return;
        }
        
        AVObject *user = [AVObject objectWithClassName:@"MyUser" objectId:[MyUser sharedUser].userId];
        [user setObject:url forKey:@"headImage"];
        [AVRequest avSaveWithObj:user succeed:^(BOOL succeed, NSError *error) {
            headSueeccd(url, succeed, error);
        }];
    }];
}

+ (void)avPasswordWithPassword:(NSString *)password pSucceed:(AVSucceed)pSucceed {
    AVObject *user = [AVObject objectWithClassName:@"MyUser" objectId:[MyUser sharedUser].userId];
    [AVRequest avSaveWithObj:user succeed:^(BOOL succeed, NSError *error) {
        pSucceed(succeed, error);
    }];
}

+ (void)avNameWithName:(NSString *)name block:(AVChangeName)block {
    AVQuery *query = [AVQuery queryWithClassName:@"MyUser"];
    [query whereKey:@"objectId" notEqualTo:[MyUser sharedUser].userId];
    [query whereKey:@"name" equalTo:name];
    
    [AVRequest avQueryWithQuery:query queryArr:^(NSArray *objects, NSError *error) {
        if (error) {
            block(3, error);
            return;
        }
        // 昵称已存在
        if (objects.count != 0) {
            block(2, error);
            return;
        }
        
        AVObject *user = [AVObject objectWithClassName:@"MyUser" objectId:[MyUser sharedUser].userId];
        [user setObject:name forKey:@"name"];
        
        [AVRequest avSaveWithObj:user succeed:^(BOOL succeed, NSError *error) {
            if (error) {
                block(3, error);
                return;
            }
            
            if (succeed) {
                block(0, error);
            }else {
                block(1, error);
            }
        }];
    }];
}

+ (void)avSexWithSex:(NSString *)sex sSueeccd:(AVSucceed)sSucceed {
    AVObject *user = [AVObject objectWithClassName:@"MyUser" objectId:[MyUser sharedUser].userId];
    [user setObject:sex forKey:@"sex"];
    [AVRequest avSaveWithObj:user succeed:^(BOOL succeed, NSError *error) {
        sSucceed(succeed, error);
    }];
}

+ (void)avFeedbackWithContent:(NSString *)content aSucceed:(AVSucceed)aSucceed {
    AVObject *user = nil;
    // 当是登录状态
    if ([MyUser sharedUser].isSignIn) {
        user = [AVObject objectWithClassName:@"MyUser" objectId:[MyUser sharedUser].userId];
    }
    
    AVObject *obj = [AVObject objectWithClassName:@"Feedback"];
    [obj setObject:user forKey:@"user"];
    [obj setObject:content forKey:@"content"];
    
    [AVRequest avSaveWithObj:obj succeed:^(BOOL succeed, NSError *error) {
        aSucceed(succeed, error);
    }];
}

@end











